<?php

namespace App\Services\Mq;

//mq推送
use App\Models\InviteDB\AppUser;
use App\Models\InviteDB\AppUser1;
use App\Models\MainDB\ChannelUser;
use App\Models\MainDB\Lottery\User;
use App\Models\MainDB\UserChannel;
use App\Repositories\LogRepository;
use App\Exceptions\BasicException;

class RabbitMqPushService
{
    //用户登陆推送
    public static function userLoginPush($params)
    {

        //更新用户表
        $userModel = ChannelUser::where(['user_id' => $params['user_code']])->first();
        if ($userModel) {
            $userModel->last_login_time = date("Y-m-d H:i:s", $params['login_time']);
            $userModel->save();
        }

//        $exchange=RabbitEventEnum::EXECHAGE_USER;
//        $route=RabbitEventEnum::ROUTE_CHANNEL;
//        $queue=RabbitEventEnum::QUEUE_USER_EVENT;
//        $sendData=[
//            'event'=>"user_login",
//            'params'=>$params
//        ];
//        RabbitmqService::push($queue,$exchange,$route,json_encode($sendData));
    }


    //用户绑定推送
    public static function userBindPush($params, $channelInfo)
    {
        // 添加到 心邀请静态去 用户列表   所有那边使用的都是 只读账号
//        $AppUser = new AppUser();
//        $AppUser1 = new AppUser1();
        $data = [
//            'app_id' => 1,
            'app_user_id' => $params['app_user_id'],
            'user_name' => $params['nickname'],
            'DisplayId' => $params['user_code'],
            'channel_no' => $params['channel_no'],
            'BindInviteUserId' => '',   //  展示不用
            'BindDisplayId' => '', // 绑定人id channel_no  de id
            'OnlineTime' => 0, //  定时器去更新
            'created_at' => date("Y-m-d H:i:s", $params['bind_time']),
            'bind_time' => date("Y-m-d H:i:s", $params['bind_time']),
            'bind_type' => $params['bind_type'],
            'bind_status' => $params['bind_status'],
            'refuse_text' => $params['refuse_text'] ?? "",
            'uni_id' => '1_' . $params['app_user_id'],
            'register_time' => date("Y-m-d H:i:s", $params['register_time']),
            'last_login_time' => date("Y-m-d H:i:s", $params['last_login_time']),
            'room_code' => '',// 作废
            'GuildId' => '',// 作废
            'GuildRole' => 0,// 作废
            'GuildDisplayId' => '',// 作废
            'InviteRoom' => '',// 作废
            'RoomDisplayId' => '',// 作废
            'CurrMachineCode' => '',// 当前为注册 定时器去更新
            'CurrIP' => '',//  当前为注册 定时器去更新
            'is_main' => $params['is_main'] ?? 1,
            'created_time' => time(),
            'mobile' => $params['mobile'] ?? ''
//        $m->room_code = $value['RoomId'];  // 还有个字段 InviteRoom 代写
//        $m->GuildId = $value['GuildId'];
//        $m->GuildRole = $value['GuildRole'];
//        $m->GuildDisplayId = $value['GuildDisplayId'] ?? '';
//        $m->InviteRoom = $value['InviteRoom'];
//        $m->RoomDisplayId = $value['RoomDisplayId'];
        ];
        if(AppUser::query()->where('app_user_id',$params['app_user_id'])->first()){
            throw new BasicException(-1, "邀请渠道已绑定");
        }
        AppUser::query()->create($data);
        AppUser1::query()->create($data);
        // 进行添加 用户 redis  用户
        $redis = app('redis')->connection('invite');
        $user_key = "user:invite:id";
        if ($redis->exists($user_key)) {
            $redis->rpush($user_key,$params['app_user_id']);
        }else{
            $arr = AppUser::query()->pluck('app_user_id')->toArray();
            $redis->rpush($user_key,$arr);
        }
//        $userModel= new ChannelUser();
//        $userModel->app_id=1;
//        $userModel->channel_no=$params['channel_no'];
//        $userModel->user_id=$params['user_code'];
//        $userModel->user_name=$params['nickname'];
//        $userModel->room_code=$params['room_code'];
//        $userModel->bind_time= date("Y-m-d H:i:s",$params['bind_time']);
//        $userModel->bind_type=$params['bind_type'];
//        $userModel->bind_status=$params['bind_status'];
//        $userModel->achievement_end_time= date("Y-m-d H:i:s",($params['register_time']+90*24*60*60));
//        $userModel->register_time= date("Y-m-d H:i:s",$params['register_time']);
//        $userModel->last_login_time= date("Y-m-d H:i:s",$params['last_login_time']);
//        $userModel->created_at= date("Y-m-d H:i:s");
//        $userModel->refuse_text=$params['refuse_text']??"";
//        $userModel->uni_id="1_".$params['parent_user_id'];
//        $userModel->is_main=$params['is_main']??1;
//        $userModel->app_user_id=$params['app_user_id'];
//        $userModel->parent_user_id=$params['parent_user_id'];
//        $userModel->save();
//
//        $user_channel= new UserChannel();
//        $user_channel->user_id = $params['app_user_id'];
//        $user_channel->channel_id = $channelInfo['id'];
//        $user_channel->channel_company_id = $channelInfo['channel_company_id'];
//        $user_channel->room_code = $channelInfo['room_code'];
//        $user_channel->save();
//        //更新用户表                                                 500 *
//        $userModel= new ChannelUser();
//        $userModel->user_id=$params['user_code'];
//        $userModel->app_id=$params['app_id'];
//        $userModel->user_name=$params['nickname'];
//        $userModel->achievement_end_time= date("Y-m-d H:i:s",($params['register_time']+90*24*60*60));
//        $userModel->register_time= date("Y-m-d H:i:s",$params['register_time']);
//        $userModel->save();

//        $exchange=RabbitEventEnum::EXECHAGE_USER;
//        $route=RabbitEventEnum::ROUTE_CHANNEL;
//        $queue=RabbitEventEnum::QUEUE_USER_EVENT;
//        $params['app_id']=1;
//        $sendData=[
//            'event'=>"user_register",
//            'params'=>$params
//        ];
//        RabbitmqService::push($queue,$exchange,$route,json_encode($sendData));
    }

    //渠道激活
    public static function userUpdateBindPush($params)
    {
        $data = [
            'refuse_text' => $params['refuse_text'] ?? '',
            'parent_user_id' => $params['parent_user_id'] ?? 0,
            'room_code' => $params['room_code'],
            'bind_status' => $params['bind_status'],
            'last_login_time' => date("Y-m-d H:i:s", $params['last_login_time']),
            'bind_time' => date("Y-m-d H:i:s", $params['bind_time']),
            'is_main' => $params['is_main'],
        ];
        if (isset($params['channel_no']) && $params['channel_no']) {
            $data['channel_no'] = $params['channel_no'];
        }

        AppUser::query()->where(['app_user_id' => $params['user_id'], 'app_id' => 1])->update($data);
        AppUser1::query()->where(['app_user_id' => $params['user_id'], 'app_id' => 1])->update($data);
//        $userModel = ChannelUser::where(['user_id' => $params['user_code'], 'app_id' => 1])->first();
//        if (!$userModel) {
//            return false;
//        }
//        $userModel->refuse_text = $params['refuse_text'] ?? '';
//        $userModel->parent_user_id = $params['parent_user_id'] ?? 0;
//        $userModel->room_code = $params['room_code'];
//        $userModel->bind_status = $params['bind_status'];
//        $userModel->last_login_time = date("Y-m-d H:i:s", $params['last_login_time']);
//        $userModel->bind_time = date("Y-m-d H:i:s", $params['bind_time']);
//        if (isset($params['channel_no']) && $params['channel_no']) {
//            $userModel->channel_no = $params['channel_no'];
//        }
//        $userModel->is_main = $params['is_main'];
//
//        $userModel->save();

    }

//
//    //用户绑定推送
//    public static  function userBindPush($params)
//    {
//        //更新用户表
//        $userModel= ChannelUser::where('user_id',$params['user_code'])->first();
//        if($userModel && !$userModel->bind_status){
//            $userModel->bind_type=$params['bind_type'];
//            $userModel->bind_time=date("Y-m-d H:i:s",$params['bind_time']);
//            if(isset($params['channel_no']) || $params['channel_no'])
//            {
//                $userModel->channel_no=$params['channel_no'];
//            }
//            $userModel->bind_status=isset($params['bind_status'])?$params['bind_status']:1;
//            $userModel->save();
//        }
//
//
//        //放弃MQ消息
////        $exchange=RabbitEventEnum::EXECHAGE_USER;
////        $route=RabbitEventEnum::ROUTE_CHANNEL;
////        $queue=RabbitEventEnum::QUEUE_USER_EVENT;
////        $params['app_id']=1;
////        $sendData=[
////            'event'=>"user_bind",
////            'params'=>$params
////        ];
////        RabbitmqService::push($queue,$exchange,$route,json_encode($sendData));
//    }


}
